package myclient;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;

import javax.swing.*;
/**
 * @author JIAO
 */
public class RegisterFrame extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField_2;
	private JTextField textField_1;
	private JTextField textField_Name;
	int sex = 1;//男为1,女为0
	/**
	 * Launch the application
	 * @param args
	 */

	/**
	 * Create the frame
	 */
	public RegisterFrame() {
		super();
		this.setResizable(false);
		getContentPane().setLayout(null);
		setBounds(500, 100, 500, 560);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		final JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBounds(51, 52, 420, 400);
		getContentPane().add(panel);

		final JLabel label = new JLabel();
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setText("昵称：");
		label.setBounds(26, 37, 80, 30);
		panel.add(label);

		final JLabel label_1 = new JLabel();
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setText("密码：");
		label_1.setBounds(26, 114, 80, 30);
		panel.add(label_1);

		final JLabel label_2 = new JLabel();
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setText("确认密码：");
		label_2.setBounds(26, 194, 80, 30);
		panel.add(label_2);
		
		final JLabel label_3 = new JLabel();
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setText("性别:");
		label_3.setBounds(26, 274, 80, 30);
		panel.add(label_3);

		textField_Name = new JTextField();
		textField_Name.setBounds(141, 37, 148, 30);
		panel.add(textField_Name);

		textField_1 = new JTextField();
		textField_1.setBounds(141, 114, 148, 30);
		panel.add(textField_1);

		textField_2 = new JTextField();
		textField_2.setBounds(141, 194, 148, 30);
		panel.add(textField_2);

		ButtonGroup group = new ButtonGroup();
		JRadioButton radioBt1 = new JRadioButton("男",true);
		JRadioButton radioBt2 = new JRadioButton("女");
		radioBt1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				sex = 1;
			}
			
		});
		radioBt2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				sex = 0;
			}});
		group.add(radioBt1);
		group.add(radioBt2);
		
		JPanel pn = new JPanel();
		pn.setLayout(new GridLayout(1,2));
		pn.add(radioBt1);
		pn.add(radioBt2);
		pn.setBounds(141, 264, 148, 30);
		panel.add(pn);
		
		final JButton button = new JButton();
		button.setText("注册");
		button.setBounds(100, 340, 106, 28);
		panel.add(button);
		
		button.addActionListener(this);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if(!textField_1.getText().trim().equals(textField_2.getText().trim()))
			return;
		String sName = textField_Name.getText().trim();
		String passWord = textField_1.getText().trim();
		try {
			Socket s = new Socket("localhost",8999);
			PrintWriter writer = new PrintWriter(s.getOutputStream(),true);
			writer.println(sName);
			writer.println(passWord);
			writer.println(sex);
			BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String id = reader.readLine();
//			String id = reader.readLine();
			writer.close();
			reader.close();
			s.close();
			JOptionPane.showMessageDialog(null, "您已经注册成功，您的号码是："+id);
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}

}
